package com.wg.dome.handle;

import com.wg.core.ioc.WgIoc;
import com.wg.dome.ClientPeer;
import com.wg.dome.ErrorCode;
import com.wg.dome.ErrorHelper;
import com.wg.dome.entity.user.AccountEntity;
import com.wg.dome.entity.user.HeroEntity;
import com.wg.dome.entity.user.PlayerEntity;
import com.wg.dome.operation.CreatePlayerRequest;
import com.wg.dome.operation.CreatePlayerResponse;
import com.wg.dome.repository.AccountRepository;
import com.wg.dome.repository.HeroRepository;
import com.wg.dome.repository.NameRepository;
import com.wg.dome.repository.PlayerRepository;
import com.wg.game.IdGenerator;
import com.wg.core.net.OperationRequest;
import com.wg.core.net.OperationResponse;
import com.wg.core.net.SendParameters;

/**
 * 创建玩家
 */
public class HandleRequest2 extends BaseHandle {

    @Override
    public OperationResponse execute(ClientPeer peer, OperationRequest request, SendParameters sendParameters) {
        CreatePlayerRequest createPlayerRequest = new CreatePlayerRequest(request);
        if (!createPlayerRequest.isValid()) {
            return ErrorHelper.getInvalidResponse(createPlayerRequest);
        }

        AccountRepository accountRepository = new AccountRepository();
        AccountEntity accountEntity = accountRepository.get(createPlayerRequest.getClientId());
        if (accountEntity == null || accountEntity.getPlayerId() > 0) {
            return ErrorHelper.getErrorResponse(request.getOperationCode(), ErrorCode.PLAYER_EXIST);
        }

        NameRepository nameRepository = new NameRepository();
        if (nameRepository.get(createPlayerRequest.getName()) != null) {
            return ErrorHelper.getErrorResponse(request.getOperationCode(), ErrorCode.PLAYER_NAME_REPEAT);
        }

        IdGenerator idGenerator = WgIoc.get(IdGenerator.class);

        PlayerRepository playerRepository = new PlayerRepository();
        PlayerEntity playerEntity = new PlayerEntity();
        playerEntity.setId(idGenerator.nextId());
        playerEntity.setName(createPlayerRequest.getName());
        playerEntity.setGold(99999);
        playerEntity.setXw(99999);
        playerEntity.setRecruitTime(0);
        playerEntity.setSurplus(10);
        playerRepository.add(playerEntity);

        HeroRepository heroRepository = new HeroRepository();
        HeroEntity heroEntity = new HeroEntity();
        heroEntity.setId(idGenerator.nextId());
        heroEntity.setsId(1);
        heroEntity.setPlayerId(playerEntity.getId());
        heroEntity.setSzPos((byte) 1);
        heroEntity.setBzPos((byte) 1);
        heroRepository.add(heroEntity);

        accountEntity.setPlayerId(playerEntity.getId());
        accountRepository.update(accountEntity);
        nameRepository.add(createPlayerRequest.getName());
        peer.setPlayerId(playerEntity.getId());

        CreatePlayerResponse createPlayerResponse = new CreatePlayerResponse();
        createPlayerResponse.setPlayerId(accountEntity.getPlayerId());
        return new OperationResponse(request.getOperationCode(), createPlayerResponse);
    }
}
